package cn.dankal.flyaudio.personal;

import android.view.View;

import com.alibaba.fastjson.JSON;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import api.UserServiceFactory;
import cn.dankal.basiclib.DKUserManager;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.pojo.UserInfoBean;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.business.pojo.ReasonBean;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.InterastAdapter;

public class SetInterastActivity extends BaseActivity {

    private RecyclerView recyclerView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_interast_set;
    }

    @Override
    protected void initComponents() {
        addSingleTitleBar("兴趣爱好");
        String interest = DKUserManager.getUserInfo().getInterestTemp();

        recyclerView = findViewById(R.id.recyclerview);
        GridLayoutManager llm = new GridLayoutManager(this, 6);
        llm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                //1232321
                if (position == 0) {
                    return 6;
                }
                if (position > 0 && position <= 2) {
                    return 3;
                }
                if (position > 2 && position <= 5) {
                    return 2;
                }
                if (position > 5 && position <= 7) {
                    return 3;
                }
                if (position > 7 && position <= 10) {
                    return 2;
                }
                if (position > 10 && position <= 12) {
                    return 3;
                }
                if (position == 13) {
                    return 6;
                }
                return 6;
            }
        });
        recyclerView.setLayoutManager(llm);
        InterastAdapter interastAdapter = new InterastAdapter();

//        String interTemp = DKUserManager.getUserInfo().getInterestTemp();
        recyclerView.setAdapter(interastAdapter);
        interastAdapter.addData("家纺家居");
        interastAdapter.addData("汽车");
        interastAdapter.addData("鞋类");
        interastAdapter.addData("运动");
        interastAdapter.addData("办公");
        interastAdapter.addData("个户");
        interastAdapter.addData("箱包");
        interastAdapter.addData("居家百货");
        interastAdapter.addData("配饰");
        interastAdapter.addData("玩具");
        interastAdapter.addData("家用电器");
        interastAdapter.addData("进口商品");
        interastAdapter.addData("美妆");
        interastAdapter.addData("美食");


        String inter = DKUserManager.getUserInfo().getInterest();
        List<String> l = JSON.parseArray(inter,String.class);
        if(inter !=null && !inter.isEmpty()){
            interastAdapter.setDefault(l);
        }
//        if (!StringUtils.isEmpty(inter)) {
//            interastAdapter.setDefault(JSON.parseArray(inter, String.class));
//        }
        findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, Object> params = new HashMap<>();
                if (interastAdapter.getSelectData().isEmpty()) {
                    ToastUtils.showShort("请选择兴趣爱好");
                    return;
                }
                params.put("interest", interastAdapter.getSelectData());
                UserServiceFactory.changeInterest(params)
                        .subscribe(new AbstractSubscriber<ReasonBean>(SetInterastActivity.this) {
                            @Override
                            public void onNext(ReasonBean reasonBean) {
                                ToastUtils.showShort("保存成功");
                                UserInfoBean userInfoBean = DKUserManager.getUserInfo();
//                                StringBuilder stringBuilder = new StringBuilder();
//                                for (String str :interastAdapter.getSelectData()){
//                                    stringBuilder.append(str).append(",");
//                                }
//                                if(stringBuilder.length()>0){
//                                    stringBuilder.deleteCharAt(stringBuilder.length()-1);
//                                }
                                userInfoBean.setInterest(JSON.toJSONString(interastAdapter.getSelectData()));
                                DKUserManager.updateUserInfo(userInfoBean);
                                LiveEventBus.get("update_user_info", String.class).post("");
                                finish();
                            }
                        });
            }
        });
    }
}
